package com.github.ecbp.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.ecbp.common.constant.ConstantCode;
import com.github.ecbp.common.data.entity.BaseEntity;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.user.service.dto.ResourceDto;
import com.github.ecbp.user.service.entity.TResource;
import com.github.ecbp.user.service.mapper.ResourceMapper;
import com.github.ecbp.user.service.response.ResourceRes;
import com.github.ecbp.user.service.service.ResourceService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@Service
public class ResourceServiceImpl extends BaseServiceImpl<ResourceMapper, TResource> implements ResourceService {

    @Override
    public List<ResourceRes> listAllData() {
        final LambdaQueryWrapper<TResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TResource::getStatus, ConstantCode.TRUE);
        return TransformUtils.listCopy(baseMapper.selectList(wrapper), ResourceRes.class);
    }

    @Override
    public List<ResourceRes> listByPermissionId(BaseIdDTO param) {
        final LambdaQueryWrapper<TResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TResource::getPermissionId, param.getId());
        wrapper.eq(TResource::getStatus, ConstantCode.TRUE);
        return TransformUtils.listCopy(baseMapper.selectList(wrapper), ResourceRes.class);
    }

    @Override
    public Boolean saveInfo(ResourceDto resourceDTO) {
        TResource resource = TransformUtils.copy(resourceDTO, TResource.class);
        resource.setStatus(ConstantCode.TRUE);
        Integer size = checkExists(resource);
        if (size > 0) {
            return false;
        }
        return super.saveOrUpdateBase(resource);
    }

    private Integer checkExists(TResource resource) {
        LambdaQueryWrapper<TResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TResource::getProjectName, resource.getProjectName());
        wrapper.eq(TResource::getUrl, resource.getUrl());
        wrapper.eq(TResource::getStatus, ConstantCode.TRUE);
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public Set<ResourceRes> listAll() {
        QueryWrapper<TResource> wrapper = new QueryWrapper<>();
        wrapper.select("r.id", "r.project_name", "r.name", "r.url", "r.description", "r.status", "p.id permissionId",
                "p.power permissionPower", "r.permission_status permissionStatus");
        wrapper.eq("r.status", ConstantCode.TRUE);
        wrapper.eq("p.status", ConstantCode.TRUE);
        wrapper.eq("r.delete_status", ConstantCode.TRUE);
        wrapper.eq("p.delete_status", ConstantCode.TRUE);
        wrapper.orderByAsc("r.create_time");
        return baseMapper.getResourceAll(wrapper);
    }

    @Override
    protected void assemble(TResource entity, LambdaQueryWrapper<TResource> wrapper) {
        super.assemble(entity, wrapper);
        if (!StringUtils.isEmpty(entity.getName())) {
            wrapper.likeRight(TResource::getName, entity.getName());
        }
        if (!StringUtils.isEmpty(entity.getUrl())) {
            wrapper.likeRight(TResource::getUrl, entity.getUrl());
        }
        if (!StringUtils.isEmpty(entity.getProjectName())) {
            wrapper.likeRight(TResource::getProjectName, entity.getProjectName());
        }
        if (null != entity.getPermissionId()) {
            wrapper.eq(TResource::getPermissionId, entity.getPermissionId());
        }
        if (null != entity.getPermissionStatus()) {
            wrapper.eq(TResource::getPermissionStatus, entity.getPermissionStatus());
        }
        wrapper.orderByDesc(BaseEntity::getCreateTime);
    }
}
